package cn.zshprint.serein.service.impl;

import cn.zshprint.serein.exception.ServiceException;
import cn.zshprint.serein.vo.PermissionRoleRelationSaveBatchVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zshprint.serein.entity.PermissionRoleRelation;
import cn.zshprint.serein.service.PermissionRoleRelationService;
import cn.zshprint.serein.mapper.PermissionRoleRelationMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class PermissionRoleRelationServiceImpl extends ServiceImpl<PermissionRoleRelationMapper, PermissionRoleRelation>
        implements PermissionRoleRelationService {


    @Override
    public List<Integer> permissionRoleRelationService(PermissionRoleRelationSaveBatchVO permissionRoleRelationSaveBatchVO) {
        List<PermissionRoleRelation> collect = permissionRoleRelationSaveBatchVO.getPermissions()
                .stream()
                .map(item -> {
                    PermissionRoleRelation permissionRoleRelation = new PermissionRoleRelation();
                    permissionRoleRelation.setRoleId(permissionRoleRelationSaveBatchVO.getRoleId());
                    permissionRoleRelation.setPermissionId(item);
                    return permissionRoleRelation;
                })
                .collect(Collectors.toList());
        boolean b = saveBatch(collect);
        if (b) {
            return collect.stream()
                    .map(PermissionRoleRelation::getId)
                    .collect(Collectors.toList());
        } else {
            throw new ServiceException("批量插入错误", -1);
        }
    }
}




